#include<bits/stdc++.h>
#define ll long long
#define re register
using namespace std;
template <typename T> inline void rd(T &x){
	x=0;int f=1;char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+c-'0';
	x*=f;
}
int T;
int n;
int a[200010];
ll f[200010],g[200010],ans,max1,max2;
int la[1000010],l[200010];
int main(){
	freopen("color.in","r",stdin);
	freopen("color.out","w",stdout);
	rd(T);
	while(T--){
		rd(n);ans=0;max1=max2=0;
		memset(f,0,sizeof(f));
		memset(g,0,sizeof(g));
		memset(la,0,sizeof(la));
		memset(l,0,sizeof(l));
		rd(a[1]);
		for(re int i=2;i<=n;i++){
			rd(a[i]);
			if(a[i]==a[i-1])n--,i--,ans+=a[i];
		}
		for(re int i=1;i<=n;i++){
			if(la[a[i]]!=0)l[i]=la[a[i]];
			la[a[i]]=i;
		}
		for(re int i=3;i<=n;i++){
			if(!l[i]){
				f[i]=max(0ll,max2);
				g[i]=max(0ll,max1);
				max1=max(max1,f[i]);
				max2=max(max2,g[i]);
			}
			else{
				f[i]=max(max2,g[l[i]+1]+a[i]);
				g[i]=max(max1,f[l[i]+1]+a[i]);
				max1=max(max1,f[i]);
				max2=max(max2,g[i]);
			}
		}
		cout<<max(max1,max2)+ans<<endl;
	}
	return 0;
}
